package com.yxj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxj.domain.PmsCategory;
import com.yxj.mapper.PmsCategoryMapper;
import com.yxj.service.IPmsCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author yxj
 * @since 2023-03-18
 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategory> implements IPmsCategoryService {

    @Override
    public List<PmsCategory> find() {

        QueryWrapper<PmsCategory> Wrapper = new QueryWrapper<>();
        Wrapper.orderByAsc("sort");
        //查询所有数据
        List<PmsCategory> pmsCategories = baseMapper.selectList(Wrapper);

        //查询所有一级菜单
        List<PmsCategory> list = new ArrayList<>();
        for (PmsCategory pmsCategory : pmsCategories) {
            if (pmsCategory.getParentCid() == 0) {
                list.add(pmsCategory);
            }
        }

        for (PmsCategory pmsCategory : list) {
            pmsCategory.setChildren(getChildren(pmsCategory, pmsCategories));
        }

        return list;
    }

    private List<PmsCategory> getChildren(PmsCategory root, List<PmsCategory> all) {
        List<PmsCategory> list = new ArrayList<>();
        for (PmsCategory pmsCategory : all) {
            if (pmsCategory.getParentCid() == root.getCatId()) {
                list.add(pmsCategory);
                pmsCategory.setChildren(getChildren(pmsCategory, all));
            }
        }

        return list;
    }


}
